home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue47 / misc / reid / groundskeeper
Encoding:
Text File  |  2002-08-14  |  1.5 KB  |  68 lines

  1. #!/bin/bash
  2.  
  3. # groundskeeper - run the argument later, when the computer isn't too
  4. # busy, and only if it really needs doing. 
  5.  
  6. # Designed to make cron more useful for computers that aren't on all
  7. # the time, and to replace run-parts in Red Hat (5.1)'s standard
  8. # /etc/crontab for running daily, weekly, and monthly scripts
  9. # in directories.
  10.  
  11. # i.e. if you want the jobs in /etc/cron.daily done daily,
  12. # but don't keep the computer on all day, have cron hourly run
  13. # "groundskeeper 1 /etc/cron.daily"
  14.  
  15. # example /etc/crontab:
  16. #SHELL=/bin/bash
  17. #PATH=/sbin:/bin:/usr/sbin:/usr/bin
  18. #MAILTO=root
  19. #
  20. ## run-parts
  21. #01 * * * * root run-parts /etc/cron.hourly
  22. #01 * * * * root /usr/local/bin/groundskeeper 1 /etc/cron.daily
  23. #01 * * * * root /usr/local/bin/groundskeeper 7 /etc/cron.weekly
  24. #01 * * * * root /usr/local/bin/groundskeeper 30 /etc/cron.monthly
  25.  
  26. # with ls-F /etc/cron.* =
  27. #cron.daily:
  28. #logrotate*      tetex.cron*     tmpwatch*       updatedb.cron*
  29. #
  30. #cron.hourly:
  31. #
  32. #cron.monthly:
  33. #
  34. #cron.weekly:
  35. #makewhatis.cron*
  36.  
  37.  
  38. # keep going when something fails
  39. set +e
  40.  
  41. if [ $# -lt 2 ]; then
  42.     echo "Usage: groundskeeper <interval in days> <dir>"
  43.     exit 1
  44. fi
  45.  
  46. if [ ! -d $2 ]; then
  47.     echo "Not a directory: $2"
  48.     exit 1
  49. fi
  50.  
  51. for i in $2/* ; do
  52.     if [ -x $i ]; then
  53.         diri=/var/spool/cron/`/usr/bin/dirname $i`
  54.     if [ ! -d $diri ]; then
  55.         mkdir -p $diri
  56.     fi
  57.     istamp=`/bin/basename $i`.lastdone
  58.     beendone=`find $diri -name $istamp -mtime -$1 -print`
  59.     if [ -z $beendone ]; then
  60.         batch -f $i
  61.         touch /var/spool/cron/$i.lastdone
  62.     fi
  63.     fi
  64. done
  65.  
  66. exit 0
  67.  
  68.